package org.lw.springbootlw.common.mapper.auth;


import org.apache.ibatis.annotations.*;
import org.lw.springbootlw.common.model.Permission;
import org.lw.springbootlw.common.model.Role;

import java.util.List;

@Mapper
public interface RoleMapper {

    // 根据用户ID查询角色
    @Select("SELECT r.id, r.role_name FROM Roles r " +
            "JOIN UserRoles ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    @ResultMap("permissionResultMap")
    List<Role> findRolesByUserId(@Param("userId") Long userId);

    // 查询角色及其权限
    @Select("SELECT r.id, r.role_name FROM Roles r WHERE r.id = #{roleId}")
    @Results(id = "permissionResultMap",value = {
            @Result(property = "id", column = "id"),
            @Result(property = "roleName", column = "role_name"),
            @Result(property = "permissions", column = "id",
                    many = @Many(select = "org.lw.springbootlw.common.mapper.auth.PermissionMapper.findPermissionsByRoleId"))
    })
    Role findRoleWithPermissionsById(@Param("roleId") Long roleId);


}

